<template>
  <div class="comment-wrapper"
       :style="{paddingLeft:level*20+'px'}"
  >
    <div class="comment-list relative-full" v-for="(msg,i) in comment" :key="i">
      <div class="msg-container pb-2">
        <div class="sender-image-box">
        <span class="sender-image">
          <img :src="msg.user.head" alt="">
        </span>
        </div>

        <div class="msg-info">
          <div class="send-info">
            <span class="sender-name" :class="{'is-me':msg.user.id===sender.id}">{{ msg.user.name }}</span>
            <span class="send-time" v-if="msg.sendTime">{{ formatSendTime(msg.sendTime) }}</span>
          </div>
          <div class="mt-5" style="font-size: 15px;color: #5a5a5a" v-html="msg.content">
          </div>
        </div>
      </div>

      <div class="comment-below-btns" v-if="level<3">
        <span class="far fa-comment pointer reply-btn"
              :class="{active:msg.id===commentCanReply.id}"
              @click="makeCommentCanReply(msg)"> 回复</span>
      </div>

      <reply-box v-if="msg.id===commentCanReply.id"
                 :articleId="articleId"
                 :parent="msg"
                 :level="level+1"
                 @afterComment="afterComment($event,msg)"
      ></reply-box>

      <div class="horizontal-line" style="margin: 16px"></div>

      <milk-comment v-if="msg.comments&&msg.comments.length>0"
                    :comment="msg.comments"
                    :articleId="articleId"
                    :sender="sender"
                    :level="level+1"
                    @updateCommentCanReply="commentCanReply={}"
      ></milk-comment>

    </div>
  </div>
</template>

<script>
import time from '../../../../libs/util.time'
import ReplyBox from "@/views/public-view/replay/replyBox/ReplyBox";

export default {
  name: 'MilkComment',
  components: { ReplyBox },
  props: {
    articleId: String,
    comment: {
      type: Array,
      default: function () {
        return []
      }
    },
    level: {
      type: Number,
      default: 0
    },
    sender: {
      type: Object,
      default: function () {
        return {}
      }
    },
  },
  data () {
    return {
      commentCanReply: {}
    }
  },
  methods: {
    makeCommentCanReply (comment) {
      this.$emit('updateCommentCanReply')
      if (this.commentCanReply.id === comment.id) {
        this.commentCanReply = {}
      } else {
        this.commentCanReply = comment
      }
    },

    afterComment (e, comment) {
      if (!comment.comments) {
        comment.comments = []
      }
      comment.comments.unshift(e)
      console.log(this.comment)
    },

    formatSendTime (_time) {
      return time.formatSendTime(_time)
    }
  }
}
</script>

<style>
.comment-below-btns {
  padding-top: 10px;
  padding-left: 56px;
}

.reply-btn{
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
}

.reply-btn:hover,
.reply-btn.active{
  background-color: #e5d8ff;
  color: white;
}
</style>
